using System;
using System.Data;
using Common;
using DAL;
using Model;

namespace BLL {
    public class MemberBLL {
        private readonly MemberDAL _memberDal = MemberDAL.GetInstance();
        
        #region 单例
        private MemberBLL(){}
        private static MemberBLL _memberBll = null;
        public static MemberBLL GetMemberBll() {
            if (_memberBll == null) {
                lock (typeof(UserBLL)) {
                    _memberBll = new MemberBLL();
                }
            }
            return _memberBll;
        }
        #endregion


        /// <summary>
        /// 查询所有会员
        /// </summary>
        /// <param name="isDeleted">表示是否被删除</param>
        /// <returns></returns>
        public DataTable GetAllMember(bool isDeleted) {
            return _memberDal.SelectAllMember(isDeleted);
        }

        public int GetPageCount(int pageSize) {
            return int.Parse(Math.Ceiling(_memberDal.CountPage(pageSize,false) / (pageSize * 1.0)).ToString());
        }

        public DataTable PagingQuery(int pageNumber, int pageSize) {
            return _memberDal.Paging(pageNumber, pageSize, false);
        }

        public Result<Member> AddMember(Member member) {
            return _memberDal.InsertMember(member) == 1 ? Result<Member>.Ok("insertion successful") : Result<Member>.Error("insertion failed");
        }

        public DataTable QueryMemberType() {
            return _memberDal.SelectMemberType();
        }

        public Result<Member> UpdateMember(Member member) {
            return _memberDal.UpdateMember(member) == 1 ? Result<Member>.Ok("update successful") : Result<Member>.Error("update failed");
        }

        public Result<Member> DeleteMember(Member member , bool isDeleted) {
            return _memberDal.TransMemberRemoveFlag(member,isDeleted) == 1 ? Result<Member>.Ok("delete successful") : Result<Member>.Error("delete failed");
        }

        public DataTable QueryByID(int mid) {
            return _memberDal.SelectByID(mid);
        }

        public DataTable QueryByName(string queryStr) {
            return _memberDal.SelectByName(queryStr);
        }

        public Member QueryByPhone(string phone) {
            return _memberDal.SelectByPhone(phone);
        }
    }
}